package com.jsp.app.controller;

import com.tamll.Type.DataType;
import com.tamll.Type.HomePage;
import com.tamll.bean.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class Li_foreHomeController {


    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("homePage")
    public ModelAndView homePage(){
        HomePage homePage=new HomePage();

        DataType forObject = restTemplate.getForObject("http://localhost:9092/sortAll", DataType.class);

        homePage.setCategoryData(forObject.getData());

        DataType forObject1 = restTemplate.getForObject("http://localhost:9092/selectListImage", DataType.class);

        homePage.setProductImage(forObject1.getProducts());

        DataType forObject2 = restTemplate.getForObject("http://localhost:9092/sortName", DataType.class);

        homePage.setCategoryData(forObject2.getData());

        DataType forObject3 = restTemplate.getForObject("http://localhost:9092/goodsCollection", DataType.class);

        homePage.setCommoditySorts(forObject3.getCommoditySorts());


        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("fore/homePage");
        modelAndView.addObject("categoryList",homePage);

        return modelAndView;
    }
    @ResponseBody
    @RequestMapping(value = "product", method = RequestMethod.GET)
    public List<Product> goToPage(Integer pid) {
        String url = "http://localhost:9092/titleCategoryId?categoryId={categoryId}";
        Map<String,Integer> map1 = new HashMap<String,Integer>();
        map1.put("categoryId",pid);
        DataType dataType = restTemplate.getForObject(url, DataType.class, map1);
        return dataType.getProducts();
    }
}
